﻿using Mohe.Scada.Util.Extensions;
using Mohe.Scada.Util.Helper;
using System.Configuration;

namespace Mohe.Scada.Config
{
    public class LocalSetting
    {
        public const string RootWindow = "Root";

        public static bool ShowCycleTime { get; set; } = ConfigHelper.GetConfigurationValue(nameof(ShowCycleTime)).ToBool();
        public static int Tenant { get; set; } = ConfigHelper.GetConfigurationValue("Tenant").ToInt();
        public static bool MultiLang { get; set; } = ConfigHelper.GetConfigurationValue(nameof(MultiLang)).ToBool();
        public static bool IsDev { get; set; } = ConfigHelper.GetConfigurationValue(nameof(IsDev)).ToBool();
        public static string Title { get; set; } = ConfigHelper.GetConfigurationValue(nameof(Title));
        public static int ProductEditWidth { get; set; } = Convert.ToInt32(ConfigHelper.GetConfigurationValue(nameof(ProductEditWidth)));
        public static int ProductEditHeight { get; set; } = Convert.ToInt32(ConfigHelper.GetConfigurationValue(nameof(ProductEditHeight)));

        public static int CanvasWidth { get; set; } = ConfigHelper.GetConfigurationValue(nameof(CanvasWidth)).ToInt();
        public static int CanvasHeight { get; set; } = ConfigHelper.GetConfigurationValue(nameof(CanvasHeight)).ToInt();
        public static string Language { get; set; } = ConfigHelper.GetConfigurationValue(nameof(Language));
        //public static double FontSize { get; set; } = Convert.ToDouble(ConfigHelper.GetConfigurationValue(nameof(FontSize)));
        //public static string FontFamily { get; set; } = ConfigHelper.GetConfigurationValue(nameof(FontFamily));
        //public static string Accent { get; set; } = ConfigHelper.GetConfigurationValue(nameof(Accent));
        //public static string Theme { get; set; } = ConfigHelper.GetConfigurationValue(nameof(Theme));

        //public static string NavigationLocation { get; set; } = ConfigHelper.GetConfigurationValue(nameof(NavigationLocation));

        //public static string NavigationAccent { get; set; } = ConfigHelper.GetConfigurationValue(nameof(NavigationAccent));

        //public static string TitleAccent { get; set; } = ConfigHelper.GetConfigurationValue(nameof(TitleAccent));

        //public static string ToolBarLocation { get; set; } = ConfigHelper.GetConfigurationValue(nameof(ToolBarLocation));

        //public static string StatusBarLocation { get; set; } = ConfigHelper.GetConfigurationValue(nameof(StatusBarLocation));

        //public static string ScreenMode { get; set; } = ConfigHelper.GetConfigurationValue(nameof(ScreenMode));
        public static string Version { get; set; } = ConfigHelper.GetConfigurationValue(nameof(Version));

        public static string Servers { get; set; } = ConfigHelper.GetConfigurationValue(nameof(Servers));

        public static string ServerIP { get; set; } = ConfigHelper.GetConfigurationValue(nameof(ServerIP));

        public static string ClientType { get; set; } = ConfigHelper.GetConfigurationValue(nameof(ClientType));


        #region TimeSeries
        public static TimeSeriesSetting TsSetting { get; set; } = ConfigHelper.GetValue<TimeSeriesSetting>("TimeSeries");
        #endregion

        //#region TcpServer
        //public static string TcpServerIp { get; set; } = ConfigHelper.GetConfigurationValue(nameof(TcpServerIp));
        //public static int TcpServerPort { get; set; } = Convert.ToInt32(ConfigHelper.GetConfigurationValue(nameof(TcpServerPort)));
        //#endregion

        #region App
        public static AppSecretSetting AppSet { get; set; } = ConfigHelper.GetValue<AppSecretSetting>("AppSetting");
        #endregion
        #region Mes
        public static bool OnLine { get; set; } = ConfigHelper.GetConfigurationValue(nameof(OnLine)).ToBool();
        #endregion
        public static Action<string> SettingChanged;

        public LocalSetting()
        {

        }
        /*
        /// <summary>
        /// 保存appSetting
        /// </summary>
        /// <param name="key">appSetting的KEY值</param>
        /// <param name="value">appSetting的Value值</param>
        public static void SetAppSetting(string key, object value)
        {

            //静态类的获取方法
            if (typeof(LocalSetting).GetProperty(key).GetValue(typeof(LocalSetting)) == value)
                return;

            // 创建配置文件对象
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.AppSettings.Settings[key] != null)
            {
                // 修改
                config.AppSettings.Settings[key].Value = value.ToString();
            }
            else
            {
                // 添加
                AppSettingsSection ass = (AppSettingsSection)config.GetSection("appSettings");
                ass.Settings.Add(key, value.ToString());
            }

            // 保存修改
            config.Save(ConfigurationSaveMode.Modified);

            // 强制重新载入配置文件的连接配置节
            ConfigurationManager.RefreshSection("appSettings");

            //静态类的设置方法
            typeof(LocalSetting).GetProperty(key).SetValue(typeof(LocalSetting), value);

            if (SettingChanged != null)
            {
                SettingChanged(key);
            }
        }
        */
        public static Configuration GetWriteSection(string key, ConfigurationSection section)
        {
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            ConfigurationSection lastsection = config.Sections[key];
            if (lastsection == null)
            {
                config.Sections.Add(key, section);
            }

            return config;
        }

        public static void SaveSection(Configuration config)
        {
            config.Save();
        }

        public static ConfigurationSection GetSection(string key)
        {
            try
            {
                return System.Configuration.ConfigurationManager.GetSection(key) as ConfigurationSection;
            }
            catch
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.Sections.Remove(key);
                config.Save();
                return null;
            }
        }
    }
}
